package com.photoeditor.demo.ui.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.PathEffect;
import android.util.AttributeSet;
import android.widget.ImageView;

import com.common.base.utils.DeviceUtils;


/**
 * 有外边框的ImageView
 * 虚线外边框
 */
public class DashedImageView extends ImageView {


    private Paint mBoundPaint;

    private boolean mShowBoundEnable = true;

    public DashedImageView(Context context) {
        super(context);
        init();
    }

    public DashedImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public DashedImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (mShowBoundEnable) {
            canvas.drawRect(canvas.getClipBounds(), mBoundPaint);
        }
    }

    public void setShowBoundEnable(boolean enable) {
        mShowBoundEnable = enable;
    }

    private void init() {
        setWillNotDraw(false);
        mBoundPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        PathEffect effect = new DashPathEffect(new float[]{8, 8}, 1);
        mBoundPaint.setAntiAlias(true);
        mBoundPaint.setColor(Color.parseColor("#ffffffff"));
        mBoundPaint.setPathEffect(effect);
        mBoundPaint.setStyle(Style.STROKE);
        mBoundPaint.setStrokeWidth(DeviceUtils.dip2px(getContext(), 1));
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        postInvalidate();
    }
}
